/**
 * Created by wlc on 2018/5/24.
 */

/**
 * 变量与对象初始化
 */
window.onload = function (){
    $('#X-ray').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#CT').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#MR').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#US').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#ECT').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#PET-CT').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#Other').filer({
        limit: 1,
        maxSize: 10,
        extensions: ['jpg', 'jpeg', 'png', 'gif'],
        changeInput: true,
        showThumbs: true,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cha are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });

    $('#boneTumor_files_userExcel').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['cif'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cif are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_x').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_ct').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_mr').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_chaosheng').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['cif'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only cif are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_ect').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_pet').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
    $('#boneTumor_files_followExcel').filer({
        limit: 300,
        maxSize: 2048,
        extensions: ['outmol'],
        changeInput: true,
        showThumbs: false,
        captions: {
            button: "上传",
            feedback: "",
            errors: {
                filesLimit: "Only {{fi-limit}} files are allowed to be uploaded.",
                filesType: "Only outmol are allowed to be uploaded.",
                filesSize: "{{fi-name}} is too large! Please upload file up to {{fi-maxSize}} MB.",
                filesSizeAll: "Files you've choosed are too large! Please upload files up to {{fi-maxSize}} MB."
            }
        }
    });
};

/**
 * 表格初始化
 */
//MSTS_table
MSTS_table = [
    ['','','','','','','']
];
$('#MSTS-table1').jexcel({
    data:MSTS_table,
    colHeaders: ['疼痛（5）','肢体功能（5）','满意度（5）','支具辅助（5） ','行走（5）','步态（5）','总分（30）'],
    colWidths: [ 100,100,100,100,100,100,100]
});


/**
 * 保存表单数据
 */
function saveBoneTumor() {
    var i = 0;
//    part1
    var USER_INFORMATION = document.getElementsByName("BONETUMOR_USER_INFORMATION");
    var USER_INFORMATION_str = "";
    for (i = 0; i < USER_INFORMATION.length;i++){
        if(USER_INFORMATION[i].value === ""){
            USER_INFORMATION_str = USER_INFORMATION_str + "null" + 'wlcniubi';
        }else {
            USER_INFORMATION_str = USER_INFORMATION_str + USER_INFORMATION[i].value + 'wlcniubi';
        }
    }
    $("#BONETUMOR_USER_INFORMATION").val(USER_INFORMATION_str);
//    part2
    var VISIT_INFORMATION = document.getElementsByName("BONETUMOR_VISIT_INFORMATION");
    var VISIT_INFORMATION_str = "";
    for (i = 0; i < VISIT_INFORMATION.length;i++){
        if(VISIT_INFORMATION[i].value === ""){
            VISIT_INFORMATION_str = VISIT_INFORMATION_str + "null" + 'wlcniubi';
        }else {
            VISIT_INFORMATION_str = VISIT_INFORMATION_str + VISIT_INFORMATION[i].value + 'wlcniubi';
        }
    }
    $("#BONETUMOR_VISIT_INFORMATION").val(VISIT_INFORMATION_str);
//    part3
    var CHECK_INFORMATION = document.getElementsByName("BONETUMOR_CHECK_INFORMATION");
    var CHECK_INFORMATION_str = "";
    for (i = 0; i < CHECK_INFORMATION.length;i++){
        if(CHECK_INFORMATION[i].value === ""){
            CHECK_INFORMATION_str = CHECK_INFORMATION_str + "null" + 'wlcniubi';
        }else {
            CHECK_INFORMATION_str = CHECK_INFORMATION_str + CHECK_INFORMATION[i].value + 'wlcniubi';
        }
    }
    $("#BONETUMOR_CHECK_INFORMATION").val(CHECK_INFORMATION_str);
//    part4
    var SURGERY_INFORMATION = document.getElementsByName("BONETUMOR_SURGERY_INFORMATION");
    var SURGERY_INFORMATION_str = "";
    for (i = 0; i < SURGERY_INFORMATION.length;i++){
        if(SURGERY_INFORMATION[i].value === ""){
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + "null" + 'wlcniubi';
        }else {
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + SURGERY_INFORMATION[i].value + 'wlcniubi';
        }
    }
    //implantMaterial—table
    var implantMaterialBody = document.getElementById("surgical_implantMaterial_content").children[0];
    for (i = 0; i< implantMaterialBody.children.length;i++){
        var tr = implantMaterialBody.children[i];
        if (tr.children[0].children[0].checked){
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + tr.children[2].children[0].value + 'wlcniubi' + tr.children[3].children[0].value + 'wlcniubi';
        }else {
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + "nullwlcniubinullwlcniubi";
        }
    }
    //fixingMaterial-table
    var fixingMaterialBody = document.getElementById("surgical_fixingMaterial_content").children[0];
    for (i = 0; i< fixingMaterialBody.children.length;i++){
        var tr = fixingMaterialBody.children[i];
        if (tr.children[0].children[0].checked){
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + tr.children[2].children[0].value + 'wlcniubi' + tr.children[3].children[0].value + 'wlcniubi';
        }else {
            SURGERY_INFORMATION_str = SURGERY_INFORMATION_str + "nullwlcniubinullwlcniubi";
        }
    }
    $("#BONETUMOR_SURGERY_INFORMATION").val(SURGERY_INFORMATION_str);
//    part5
    var followUp_PARTs = document.getElementsByClassName("bone_tumor_followUp");
    var FOLLOWUP_INFORMATION_str = "";
    for (var j = 1; j < followUp_PARTs.length+1;j++){
        var FOLLOWUP_INFORMATION = document.getElementsByName("BONETUMOR_FOLLOWUP_PART"+j);
        for (i = 0; i < FOLLOWUP_INFORMATION.length;i++){
            if(FOLLOWUP_INFORMATION[i].value === ""){
                FOLLOWUP_INFORMATION_str = FOLLOWUP_INFORMATION_str + "null" + 'wlcniubi';
            }else {
                FOLLOWUP_INFORMATION_str = FOLLOWUP_INFORMATION_str + FOLLOWUP_INFORMATION[i].value + 'wlcniubi';
            }
        }
        var mstsTbodyTr = document.getElementById("MSTS-table"+j).children[0].children[1].children[0];
        for (i = 1; i < mstsTbodyTr.children.length;i++){
            if (mstsTbodyTr.children[i].innerText===""){
                FOLLOWUP_INFORMATION_str = FOLLOWUP_INFORMATION_str + "null" + 'wlcniubi';
            }else {
                FOLLOWUP_INFORMATION_str = FOLLOWUP_INFORMATION_str + mstsTbodyTr.children[i].innerText + 'wlcniubi';
            }
        }
        FOLLOWUP_INFORMATION_str = FOLLOWUP_INFORMATION_str + 'FOLLOWUPPART';
    }
    $("#BONETUMOR_FOLLOWUP_INFORMATION").val(FOLLOWUP_INFORMATION_str);

    document.getElementById("BONETUMOR_submit").click();
}

/**
 * 添加一个骨肿瘤随访部分
 */
function addBoneTumorFollowPart() {
    var tablist = $('#bone_tumor_followUp_information')[0].children[1];
    var tabcontent = $('#bone_tumor_followUp_information')[0].children[2];
    var listnum = tablist.children.length;
    //list
    var list = '<li role="presentation"><a href="#bone_tumor_followUp'+listnum+'" aria-controls="bone_tumor_followUp'+listnum+'" role="tab" data-toggle="tab">随访'+listnum+'<div class="del-panel" onclick="delBoneTumorFollowPart()">×</div></a></li>'
    tablist.insertBefore($(list)[0], tablist.children[listnum-1]);
    //content
    var content = '<div role="tabpanel" class="tab-pane bone_tumor_followUp" id="bone_tumor_followUp1">'+
        '<h3 style="text-align: left;margin-bottom: 20px;padding-left: 20px">随访'+listnum+'</h3>'+
        '<div class="form-inline row" id="row1">'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c1">'+
        '<label>随访时间:</label>'+
    '<input type="text" class="form-control"  name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c2">'+
        '<label>术后用药情况:</label>'+
    '<input type="text" class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '<div class="row imageData">'+
        '<div class="form-inline row" id="row1">'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c1">'+
        '<label>X线:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c2">'+
        '<label>CT:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c3">'+
        '<label>MR:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '<div class="form-inline row" id="row2">'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c1">'+
        '<label>超声:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c2">'+
        '<label>ECT:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c3">'+
        '<label>PET-CT:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '<div class="form-inline row" id="row3">'+
        '<div class="form-group col-md-12 col-sm-12 col-xs-12" id="c1">'+
        '<label>其他:</label>'+
    '<input type="text" class="form-control" readonly name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '</div>'+
        '<div class="form-inline row" id="row3">'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c1">'+
        '<label>复发:</label>'+
    '<input type="text" class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c2">'+
        '<label>转移:</label>'+
    '<select class="form-control select-altInput" name="BONETUMOR_FOLLOWUP_PART'+listnum+'" style="width: 200px;">'+
        '<option>(无)</option>'+
        '<option>肺转移</option>'+
        '<option>其他</option>'+
        '</select>'+
        '</div>'+
        '<div class="form-group col-md-4 col-sm-4 col-xs-4" id="c3">'+
        '<label>并发症:</label>'+
    '<input type="text" class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '<div class="form-inline row" id="row4">'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c1">'+
        '<label>预后:</label>'+
    '<select class="form-control select-altInput" name="BONETUMOR_FOLLOWUP_PART'+listnum+'" style="width: 200px;">'+
        '<option>(无)</option>'+
        '<option>死亡</option>'+
        '<option>带瘤生存</option>'+
        '<option>无瘤生存</option>'+
        '<option>其他</option>'+
        '</select>'+
        '</div>'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c2">'+
        '<label>特殊处理:</label>'+
    '<input type="text" class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '<div class="row MSTS">'+
        '<div class="MSTS-table" id="MSTS-table'+listnum+'"></div>'+
        '</div>'+
        '<div class="form-inline row" id="row6">'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c1">'+
        '<label>Mankin评分:</label>'+
    '<select class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'" style="width: 200px;">'+
        '<option>(无)</option>'+
        '<option>优</option>'+
        '<option>良</option>'+
        '<option>中</option>'+
        '<option>差</option>'+
        '</select>'+
        '</div>'+
        '<div class="form-group col-md-6 col-sm-6 col-xs-6" id="c2">'+
        '<label>步态图检测日期:</label>'+
    '<input type="text" class="form-control" name="BONETUMOR_FOLLOWUP_PART'+listnum+'">'+
        '</div>'+
        '</div>'+
        '</div>';
    var Dom = $(content)[0];
    Dom.id = "bone_tumor_followUp" + listnum;
    tabcontent.appendChild(Dom);
    //初始化
    $('#MSTS-table'+listnum).jexcel({
        data:MSTS_table,
        colHeaders: ['疼痛（5）','肢体功能（5）','满意度（5）','支具辅助（5） ','行走（5）','步态（5）','总分（30）'],
        colWidths: [ 100,100,100,100,100,100,100]
    });
}

/**
 * 删除一个骨肿瘤随访部分
 */
function delBoneTumorFollowPart() {
    var tablist = $('#bone_tumor_followUp_information')[0].children[1];
    var tabcontent = $('#bone_tumor_followUp_information')[0].children[2];
    if (tablist.children.length > 2){
        tablist.children[tablist.children.length-2].remove();
        tabcontent.children[tablist.children.length-1].remove();
        for (var i = 0 ;i < tablist.children.length; i++){
            tablist.children[i].className = tablist.children[i].className.replace("active","");
            tabcontent.children[i].className = tabcontent.children[i].className.replace("active","");
        }
        tablist.children[0].className +=" active";
        tabcontent.children[0].className +=" active";
    }
}



// $(document).on('click', '#upload_follow_btn', function () {
//     var file_set = $('#follow_up_excels')[0].files;
//     var files_length = file_set.length;
//
//     var formfile = new FormData();
//     for(var i = 0; i < files_length; i++) {
//         formfile.append("excel_files[]", file_set[i]);
//     }
//     $.ajax({
//         url: "../follow_up_part/upload_file",
//         data: formfile,
//         type:'POST',
//         async: false,
//         cache: false,
//         contentType: false,
//         processData: false,
//         success:function(return_data) {
//             if(return_data === 'success') {
//                 alert("上传成功");
//             } else alert("上传失败,请检查文件后缀");
//         },
//         error:function() {
//           alert("上传失败");
//         }
//     });
// });

